package com.atguigu.phoneinteceptor.db;

import java.util.ArrayList;
import java.util.List;

import com.atguigu.phoneinteceptor.bean.BlackNumber;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class BlackNumberDao {

	private DBHelper dbHelper;

	public BlackNumberDao(Context context) {
		dbHelper = new DBHelper(context);
	}

	public int addBlackNumber(BlackNumber blackNumber) {
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("number", blackNumber.getNumber());
		values.put("name", blackNumber.getName());
		values.put("type", blackNumber.getType());
		long id = database.insert(DBHelper.TABLE_BLACK_INFO, null, values);
		database.close();
		return (int) id;
	}

	public List<BlackNumber> getAll() {
		List<BlackNumber> list = new ArrayList<BlackNumber>();
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		String sql = "select _id,number,name,type from "
				+ DBHelper.TABLE_BLACK_INFO + " order by _id desc";
		Cursor cursor = database.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			BlackNumber blackNumber = new BlackNumber();
			blackNumber.set_id(cursor.getInt(0));
			blackNumber.setNumber(cursor.getString(1));
			blackNumber.setName(cursor.getString(2));
			blackNumber.setType(cursor.getInt(3));
			list.add(blackNumber);
		}
		cursor.close();
		database.close();
		return list;
	}

	public List<BlackNumber> getBlackNumbers(String key) {
		List<BlackNumber> list = new ArrayList<BlackNumber>();
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		String sql = "select _id,number,name,type from "
				+ DBHelper.TABLE_BLACK_INFO
				+ " where number like ? or name like ?";
		Cursor cursor = database.rawQuery(sql, new String[] { "%" + key + "%",
				"%" + key + "%" });
		while (cursor.moveToNext()) {
			BlackNumber blackNumber = new BlackNumber();
			blackNumber.set_id(cursor.getInt(0));
			blackNumber.setNumber(cursor.getString(1));
			blackNumber.setName(cursor.getString(2));
			blackNumber.setType(cursor.getInt(3));
			list.add(blackNumber);
		}
		cursor.close();
		database.close();
		return list;
	}

	public void updateBlackNumber(BlackNumber blackNumber) {
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("number", blackNumber.getNumber());
		values.put("name", blackNumber.getName());
		values.put("type", blackNumber.getType());
		database.update(DBHelper.TABLE_BLACK_INFO, values, "_id="
				+ blackNumber.get_id(), null);
		database.close();
	}

	public void deleteBlackNumbers(List<Integer> ids) {
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		for (Integer id : ids) {
			database.delete(DBHelper.TABLE_BLACK_INFO, "_id=" + id, null);
		}
		database.close();
	}

	public boolean isCallBlack(String number) {
		boolean isBlack = false;
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		Cursor cursor = database.rawQuery("select count(*) from "
				+ DBHelper.TABLE_BLACK_INFO
				+ " where number=? and type in (0,1)", new String[] { number });
		if (cursor.moveToNext()) {
			int count = cursor.getInt(0);
			isBlack = (count > 0);
		}
		database.close();
		return isBlack;
	}

	public boolean isSmsBlack(String number) {

		boolean isBlack = false;
		SQLiteDatabase database = dbHelper.getWritableDatabase();

		Cursor cursor = database.rawQuery("select count(*) from "
				+ DBHelper.TABLE_BLACK_INFO
				+ " where number=? and type in (0,2)", new String[] { number });
		if (cursor.moveToNext()) {
			int count = cursor.getInt(0);
			isBlack = (count > 0);
		}
		database.close();
		return isBlack;
	}
}
